#
# Copyright (c) 2012-2014, Technical University of Denmark (DTU)
# All rights reserved.
# 
# The Flexhouse 2.0 platform ("this software") is licensed under the
# BSD 3-clause license.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#   - Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   - Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   - Neither the name of DTU nor the names of its contributors may be used to
#     endorse or promote products derived from this software without specific
#     prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE TECHNICAL UNIVERSITY OF DENMARK BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Import the PythonPluginController class
import sys
sys.path.append("..")
from PythonPluginController import *

# This simple controller demonstrates the use of a Python Plugin Controller
# @author M. Svendsen
class PythonTestController(PythonPluginController):
    def sensorUpdated(self, sensor):
    # Check if the sensor that caused this event is a button
        if (sensor.getType() == self.gateway.jvm.SystemConstants.DeviceType.Button):
            # Get the BuildingConfig
            buildingConfig = self.javaProxy.getBuildingConfig()
            # Get the room in which this button is placed
            room = buildingConfig.getRoomForSensor(sensor.getName())
            actuators = room.getActuators()
            for i in range(actuators.size()):
                act = actuators.get(i)
                # Check if actuator is of the DimmableLight type
                if (act.getType() == self.gateway.jvm.SystemConstants.DeviceType.DimmableLight):
                    # Turn the light on or off accordingly
                    self.javaProxy.setActuator(act.getName(), sensor.getValue())
                    print("Setting light: {0} to {1}".format(act.getName(), str(sensor.getValue().value)))


    def supplyControllerConfig(self, controllerConfig):
        # Request control of lights
        control = self.gateway.jvm.SystemConstants.DeviceType.DimmableLight
        controllerConfig.addControlledDeviceType(control)
        # Subscribe to events related to buttons
        subscription = self.gateway.jvm.SystemConstants.DeviceType.Button
        controllerConfig.addTypeSubscription(subscription)

    def initialStateReceived(self, actuators):
        # Get the BuildingConfig
        buildingConfig = self.javaProxy.getBuildingConfig()

        # Get the RoomConfig object for the sensor named Toilet.temp_1
        room = buildingConfig.getRoomForSensor("Toilet.temp_1")

	if room:
          # List all actuators in the toilet
          actuators = room.getActuators()
          for i in range(actuators.size()):
              print("Actuator: {0}".format(actuators.get(i).getName()))


if __name__ == '__main__':
    # Specify name and owner for this Plugin Controller
    name = "PythonTestController"
    owner = "masv"
    cfgfile = str(sys.argv[1])

    # Instatiate the controller.
    # This will block until the controller is terminated (Use CTRL-C)
    controller = PythonTestController(name, owner, cfgfile);

















